// 这里是C语言实现 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Node{
    int data;
    Node* next;
}Node;

typedef struct SingleList{
    Node* head;
}SingleList;

Node* createNode(int data){
    Node* newNode = (Node*)malloc(sizeof(Node));
    if(newNode == NULL){
        printf("segementation fault!\n");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

SingleList* createList(){
    SingleList* list = (SingleList*)malloc(sizeof(SingleList));
    if(list == NULL){
        printf("error List!");
        exit(1);
    }
    list->head = NULL;
    return list;
}

bool isEmpty(SingleList* list){
    return list->head == nullptr;
}

void insertAtHead(SingleList *list,int data){
    Node* newNode = createNode(data);
    if(isEmpty(list)){
        list->head = newNode;
        newNode->next = newNode;
        //list->head->next = list->head;
        return;
    }else{
        Node* current = list->head;
        while(current->next != list->head){
            current = current->next;
        }
        newNode->next = list->head;
        current->next = newNode;
        list->head = newNode;
    }
}

void insertAtTail(SingleList* list,int data){
    Node* newNode = createNode(data);
    if(isEmpty(list)){
        list->head = newNode;
        newNode->next = newNode;
        return;
    }
    Node* current = list->head;

    while(current->next != list->head){
        current = current->next;
    }
    current->next = newNode;
    newNode->next = list->head;
}

void deleteHead(SingleList* list){
    if(isEmpty(list)){
        printf("unflowed !\n");
        return;
    }
    Node* temp = list->head;
    if(list->head->next == list->head){
        free(temp);
        list->head = NULL;
        return;
    }

    Node* current = list->head;
    while(current->next != list->head){
        current = current->next;
    }
    list->head = temp->next;
    current->next = list->head;
    free(temp);
    
}

void deleteNode(SingleList* list,int data){
    if(isEmpty(list)){
        printf("error delete empty!\n");
        return;
    }
    if(list->head->data == data){
        deleteHead(list);
        return;
    }
    Node* current = list->head;
    Node* prev = NULL;
    do{
        if(current->next->data == data){
            Node* temp = current->next;

            if(temp->next == list->head){
                current->next = list->head;
            }else{
                current->next = temp->next;
            }
            free(temp);
            return;
        }
        current = current->next;
    }while(current != list->head);

    printf("fail to delete!!! \n");
}

void display(SingleList* list){
    if(isEmpty(list)){
        printf("list is empty!\n");
        return;
    }
    Node* current = list->head;
    while(current->next != list->head  && current->next != NULL){
        printf("%d -> ",current->data);
        current = current->next;
    }
    printf("%d -> ",current->data);
    printf("NULL\n");
}


int main(){
    SingleList* l = createList();
    insertAtHead(l,50);
    insertAtHead(l,30);
    insertAtHead(l,20);

    display(l);

    insertAtTail(l,34);
    insertAtTail(l,54);
    display(l);

    deleteHead(l);
    display(l);

    deleteNode(l,50);
    display(l);
}